package net.adetech.dcweb.core.common.rest;

import net.adetech.dcweb.core.common.entity.CommonDepartment;
import net.adetech.dcweb.core.common.service.ICommonDepartmentService;
import net.adetech.utils.UtilsCommon;
import net.adetech.utils.spring.PageBean;
import net.adetech.web.utils.spring.BaseController;
import net.adetech.web.utils.UtilsUI;
import net.adetech.web.utils.UtilsWeb;
import net.adetech.utils.UtilsCondition;
import org.springframework.ui.Model;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by auto on 2016/10/21.
 */
@RestController
public class CommonDepartmentRestController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(CommonDepartmentRestController.class);

    @Autowired
    private ICommonDepartmentService iCommonDepartmentService;

    @PostMapping(value = "/common/department", produces = "application/json")
    public String add(HttpServletRequest request, HttpServletResponse response) {
        CommonDepartment entity = this.wrapper(CommonDepartment.class, request);
        entity = iCommonDepartmentService.save(entity);
        response.setStatus(HttpServletResponse.SC_CREATED);
        opLog(request, logger, "增加", "");
        return UtilsUI.data(entity);
    }

    @GetMapping(value = "/common/department/{id}", produces = "application/json")
    public String detail(@PathVariable Long id, HttpServletResponse response, HttpServletRequest request) {
        CommonDepartment entity = iCommonDepartmentService.find(id);
        if (null == entity) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return null;
        }
        opLog(request, logger, "查看", "");
        return JSONObject.toJSONString(entity);
    }

    @GetMapping(value = "/common/adminuser/bydepartment", produces = "application/json")
    public String bydepartment(HttpServletResponse response, HttpServletRequest request) {
        List<CommonDepartment> list = iCommonDepartmentService.bydepartment();
        if (list == null || list.size() == 0) {
            JSONObject jsonObject = new JSONObject();
            return UtilsUI.fail();
        }
        opLog(request, logger, "部门分组的帐号", "");
        String jsonString = JSONObject.toJSONString(list);
        return jsonString;
    }

    @PutMapping(value = "/common/department/{id}", produces = "application/json")
    public void update(HttpServletRequest request, @PathVariable Long id, HttpServletResponse response) {
        CommonDepartment entity = this.wrapper(CommonDepartment.class, request);
        entity.setId(id);
        int rs = iCommonDepartmentService.update(entity);
        opLog(request, logger, "修改", "");
        if (rs > 0) {
            response.setStatus(HttpServletResponse.SC_CREATED);
        } else {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }

    @DeleteMapping(value = "/common/department", produces = "application/json")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String idsStr = UtilsWeb.getString(request, "ids");
        if ("".equals(idsStr)) {
            return UtilsUI.tip("请传入ids参数");
        }
        String[] ids = idsStr.split(",");
        if (null != ids && ids.length > 0) {
            List
                    <Long> idsLong = new ArrayList<>(0);
            for (int i = 0; i < ids.length; i++) {
                idsLong.add(UtilsCommon.getLong(ids[i]));
            }
            iCommonDepartmentService.delete(idsLong);
        }
        opLog(request, logger, "删除", "");
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        return null;
    }

    @GetMapping(value = "/common/departments", produces = "application/json")
    public String page(HttpServletRequest request) {
        Integer nowPage = UtilsWeb.getInteger(request, "nowPage");
        Integer pageSize = UtilsWeb.getInteger(request, "pageSize");
        if (null == nowPage || nowPage <= 0) {
            return UtilsUI.tip("未传入nowPage参数，或者nowPage参数错误！");
        }
        if (null == pageSize || pageSize <= 0) {
            return UtilsUI.tip("请传入pageSize参数，或者pageSize参数错误！");
        }
        PageBean<CommonDepartment> pageBean = iCommonDepartmentService.page(nowPage,
                pageSize, UtilsCondition.getConditionEntity(request.getParameterMap()));
        opLog(request, logger, "分页查看", "");
        return UtilsUI.datagrid(pageBean);
    }
}